<template>
  <div class="product-cards">
    <div v-for="(product, index) in products" :key="index" class="product-card">
      <div class="product-image">
        <img 
          :src="product.image" 
          :alt="product.name"
          @error="handleImageError"
        />
      </div>
      <div class="product-info">
        <h4 class="product-name">{{ product.name }}</h4>
        <p class="product-price">¥{{ product.price }}</p>
        <p class="product-desc">{{ product.desc }}</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { Product } from '../types/product';

defineProps<{
  products: Product[];
}>();

// 图片加载失败时使用默认图片
const handleImageError = (event: Event) => {
  const target = event.target as HTMLImageElement;
  target.src = 'https://images.unsplash.com/photo-1505740420928-5e560c06d30e?w=400';
};
</script>

<style scoped>
.product-cards {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 12px;
}

.product-card {
  width: 200px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  transition: all 0.3s ease;
  cursor: pointer;
}

.product-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.product-image {
  width: 100%;
  height: 200px;
  overflow: hidden;
  background: #f9fafb;
  display: flex;
  align-items: center;
  justify-content: center;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  padding: 12px;
}

.product-name {
  font-size: 14px;
  font-weight: 600;
  color: #1f2937;
  margin: 0 0 8px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-price {
  font-size: 18px;
  font-weight: 700;
  color: #ef4444;
  margin: 0 0 8px 0;
}

.product-desc {
  font-size: 12px;
  color: #6b7280;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-height: 1.5;
}
</style>

